package com.slf.flow.oss.function.extension;

import com.slf.flow.oss.model.Oss;
import com.slf.flow.oss.service.OssService;
import com.slf.flow.oss.utils.APIOSSUtil;
import com.slf.flow.oss.utils.MinioOSSUtil;
import com.slf.flow.oss.utils.OSSUtil;
import com.slf.flow.executor.FunctionExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class OssStringFunctionExtension implements FunctionExtension{
	
	private static OssService ossService;
	
	@Autowired
	private void setOssMapper(OssService ossService) {
		OssStringFunctionExtension.ossService = ossService;
	}

	@Override
	public Class<?> support() {
		return String.class;
	}
	
	public static Map<String ,Object> ossUpload(String url, String ossId, String filePath) throws Exception {
		Oss oss = ossService.getOss(ossId);
		if(url.equals("")){
			return null;
		}
		if(null == oss) {
		} else if("aliyun".equals(oss.getType())){
			return OSSUtil.uploadFileToOss(oss,url,filePath);
		} else if("minio".equals(oss.getType())){
			return MinioOSSUtil.uploadFileToOss(oss,url,filePath);
		} else if("api".equals(oss.getType())){
			return APIOSSUtil.uploadFileToOss(oss,url);
		}
		return null;
	}

	public static Map<String ,Object> ossUpload(String url, String ossId) throws Exception {
		Oss oss = ossService.getOss(ossId);
		if(url.equals("")){
			return null;
		}
		if(null == oss) {
		} else if("api".equals(oss.getType())){
			return APIOSSUtil.uploadFileToOss(oss,url);
		}
		return null;
	}
	
	public static void ossDelete(String filePath,String ossId) {
		OSSUtil.deleteFile(ossService.getOss(ossId), filePath);
	}

}
